﻿package classes{
	import flash.display.MovieClip;
	public class Foe extends MovieClip {
		private static  const ST_IDLE = 0;
		private static  const ST_HIT = 2;
		private static  const ST_ATTACK = 3;
		private static  const ST_JUMP = 4;
		private static  const ST_DIED = 5;

		public static  const D_LEFT = 4;
		public static  const D_RIGHT = 3;

		private var currentState:int;
		private var currentDirection:int;
		private var nFrameLeft:int=-1;
		private var nRepare:int=0;
		private var attackTime:int;
		private var floorY:int = 320;
		public var score:int;
		public var HP:int ;
		private var v:int = 0;

		public function Foe():void {

			currentState = ST_IDLE;
			gotoAndStop(1);
		}
		public function Init(x0:int ,y0:int, v0:int,speed:int,hp:int)
		{
			x = x0;
			y = y0;
			HP = hp;
			score = hp-speed;
			currentState = ST_IDLE;
			attackTime = speed;
			Jump(v0);
		}

		public function Update(targetX:int):void {
			

			if(currentState == ST_HIT)
			{

			}else if(currentState == ST_JUMP)
			{
				if(v > 0 && y>= floorY)
				{
					y = floorY;
					v == 0;
					Idle();
				}
				else
				{
					y+= v;
					v+=5;
				}
			}else if(currentState == ST_DIED)
			{
								
				y+= v;
				v+=5;
				return;
			}
			else if(currentState == ST_ATTACK)
			{
				
			}else if(currentState == ST_IDLE){
				if(y>= floorY)
				{
						y = floorY;
						v == 0;
				}
				else
				{
					y+= v;
					v+=5;
				}
				if(x > targetX)
				{
					Turn(D_LEFT);
				}
				else
				{
					Turn(D_RIGHT);
				}
				nRepare++;
				if(nRepare >= attackTime)
				{
					Attack();
				}
			}
			if(nFrameLeft == 0 && currentState != ST_DIED)
			{
				Idle();
			}			
			nFrameLeft--;
		}
		public function IsFire():Boolean{
			return (currentState == ST_ATTACK && nFrameLeft == 2);
		}
		public function IsOut():Boolean{
			return (currentState == ST_DIED && y > 450);
		}
		var rect:int = 0;
		public function Turn(dir:int):void {
			if (currentState != ST_IDLE || currentDirection == dir) {
				return;
			}
			if(rect<5)
			{
				rect++;
				return;
			}
			rect = 0;
			currentDirection = dir;
			if (currentDirection == D_RIGHT) {
				scaleX = -Math.abs(scaleX);
			}
			if (dir == D_LEFT) {
				scaleX = Math.abs(scaleX);
			}
			nRepare -=5;
		}
		public function Hit(dam:int):void {
			currentState = ST_HIT;
			gotoAndStop(4);
			nFrameLeft = 2;
			HP-=dam;
			if(HP<=0)
			{
				v = -10;
				currentState = ST_DIED;		
			}

		}
///		public 
		public function Attack():void {
			if(currentState != ST_IDLE)
			{
				return;
			}
			currentState = ST_ATTACK;
			gotoAndStop(3);
			nFrameLeft = 4;
		}
		public function Idle():void {
			if(currentState == ST_IDLE)
			{
				return;
			}
			currentState = ST_IDLE;
			nRepare = 0;
			gotoAndStop(1);
		}
		private function Jump(v0:int):void {
			if(currentState != ST_IDLE)
			{
				return;
			}
			currentState = ST_JUMP;
			gotoAndStop(2);
			v = v0;
		}
	}
}